//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmAmericanStepCondition.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Experimental/Finitedifferences/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Experimental::Finitedifferences::IFdmInnerValueCalculator^ calculator) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CFdmInnerValueCalculator^ _Ccalculator;
    try
    {
#ifdef HANDLE
        _phFdmAmericanStepCondition = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _Ccalculator = safe_cast<CFdmInnerValueCalculator^> (calculator);
        _Ccalculator->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _calculator = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_Ccalculator->GetShared ()); 
        _ppFdmAmericanStepCondition = new boost::shared_ptr<QuantLib::FdmAmericanStepCondition> (new QuantLib::FdmAmericanStepCondition ( _mesher,  _calculator ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_Ccalculator != nullptr) _Ccalculator->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (boost::shared_ptr<QuantLib::FdmAmericanStepCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmAmericanStepCondition = NULL;
#endif
	_ppFdmAmericanStepCondition = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (QuantLib::FdmAmericanStepCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmAmericanStepCondition = NULL;
#endif
	_ppFdmAmericanStepCondition = new boost::shared_ptr<QuantLib::FdmAmericanStepCondition> (&childNative);
    
    _FdmAmericanStepConditionOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (CFdmAmericanStepCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmAmericanStepCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmAmericanStepCondition = new boost::shared_ptr<QuantLib::FdmAmericanStepCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmAmericanStepCondition = NULL;
#endif
	if (!t->IsSubclassOf(CFdmAmericanStepCondition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (QuantLib::Handle<QuantLib::FdmAmericanStepCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmAmericanStepCondition = &childNative;
	_ppFdmAmericanStepCondition = &static_cast<boost::shared_ptr<QuantLib::FdmAmericanStepCondition>>(childNative.currentLink());
    
    _FdmAmericanStepConditionOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (QuantLib::Handle<QuantLib::FdmAmericanStepCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmAmericanStepCondition = &childNative;
	_ppFdmAmericanStepCondition = &static_cast<boost::shared_ptr<QuantLib::FdmAmericanStepCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::CFdmAmericanStepCondition (QuantLib::FdmAmericanStepCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmAmericanStepCondition = NULL;
#endif
	_ppFdmAmericanStepCondition = new boost::shared_ptr<QuantLib::FdmAmericanStepCondition> (new QuantLib::FdmAmericanStepCondition (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::~CFdmAmericanStepCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmAmericanStepCondition != NULL)
    {
	    delete _ppFdmAmericanStepCondition;
        _ppFdmAmericanStepCondition = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::!CFdmAmericanStepCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmAmericanStepCondition != NULL)
    {
	    delete _ppFdmAmericanStepCondition;
    }
}
QuantLib::FdmAmericanStepCondition& Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::GetReference ()
{
    if (_ppFdmAmericanStepCondition == NULL) throw gcnew NativeNullException ();
	return **_ppFdmAmericanStepCondition;
}
boost::shared_ptr<QuantLib::FdmAmericanStepCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::GetShared ()
{
    if (_ppFdmAmericanStepCondition == NULL) throw gcnew NativeNullException ();
	return *_ppFdmAmericanStepCondition;
}
QuantLib::FdmAmericanStepCondition* Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::GetPointer ()
{
    if (_ppFdmAmericanStepCondition == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmAmericanStepCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmAmericanStepCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::GetHandle ()
{
	if (_phFdmAmericanStepCondition == NULL)
	{
		_phFdmAmericanStepCondition = new Handle<QuantLib::FdmAmericanStepCondition> (*_ppFdmAmericanStepCondition);
	}
	return *_phFdmAmericanStepCondition;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::HasNative () 
{
	return (_ppFdmAmericanStepCondition != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmAmericanStepCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double prm1)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _prm1 = (QuantLib::Time)ValueHelper::Convert (prm1);
    	(*_ppFdmAmericanStepCondition)->applyTo ( _a,  _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmAmericanStepCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmAmericanStepCondition_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Experimental::Finitedifferences::IFdmInnerValueCalculator^ calculator)
{
    return gcnew CFdmAmericanStepCondition ( mesher,  calculator);
}
